package com.example.yan_music.widge.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.yan_music.R;
import com.example.yan_music.utils.XToast;

/**
 *  <p>
 *      自定义XDialog
 *  </p>
 */
public class XDialog extends Dialog {

    /**
     * 对话框的布局
     */
    private static int sLayoutResOperation = R.layout.layout_opinion_dialog; // 操作型对话框
    private static int sLayoutResTip = R.layout.layout_tip_dialog; // 简单的提示对话框

    public XDialog(@NonNull Context context) {
        super(context, R.style.single_dialog);
    }

    public XDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);

    }

    public XDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);

    }

    /**
     * 对话框类型枚举
     */
    public static enum  XType{
        // 确认型
        CONFIRM,
        TIP;
    }

    /**
     * 构造内部类
     */
    public static class XBuilder {
        private Context mContext;
        // 内容文本
        private String mMessage;
        // 取消选项的文本
        private String mNegativeText;
        // 确定选项的文本
        private String mPositiveText;
        // 标题文本
        private String mTitle;
        // 图标资源
        private int mIconRes;
        // 确认选项字体颜色资源
        private int mPositiveTextColor;
        // 标题字体颜色
        private int mTitleTextColor;
        // 内容字体颜色
        private int mMessageTextColor;
        // 取消选项控件
        private TextView mNegativeOption;
        // 确定选项控件
        private TextView mPositiveOption;
        // 内容控件
        private TextView mTvMessage;
        // 标题控件
        private TextView mTvTitle;
        // 图标
        private ImageView mImgIcon;
        // 输入控件
        private EditText mEdtInput;

        // 取消动作的点击接口
        private View.OnClickListener mNegativeOptionListener;
        // 确定动作的点击接口
        private View.OnClickListener mPositiveOptionListener;

        public XBuilder(Context context){
            this.mContext = context;
        }

        /**
         * 使用create方法创建一个对话框
         *
         * @param type  对话框的类型
         * @return
         */
        @SuppressLint("ResourceAsColor")
        public XDialog create(XType type){
            final XDialog dialog = new XDialog(mContext);
            View view = null;

            switch (type){
                case CONFIRM:
                    // 布局
                    view = LayoutInflater.from(mContext).inflate(sLayoutResOperation, null);
                    // N和P按钮
                    mNegativeOption = view.findViewById(R.id.negativity_text);
                    mPositiveOption = view.findViewById(R.id.positive_text);
                    // 内容控件
                    mTvMessage = view.findViewById(R.id.xdialog_message);
                    // 标题控件
                    mTvTitle = view.findViewById(R.id.xdialog_title);
                    // 图标控件
                    mImgIcon = view.findViewById(R.id.xdialog_icon);

                    if(!TextUtils.isEmpty(mMessage)){
                        mTvMessage.setText(mMessage);
                    }

                    if(!TextUtils.isEmpty(mNegativeText)){
                        mNegativeOption.setText(mNegativeText);
                    }

                    if(!TextUtils.isEmpty(mPositiveText)){
                        mPositiveOption.setText(mPositiveText);
                    }

                    if(!TextUtils.isEmpty(mTitle)){
                        mTvTitle.setText(mTitle);
                    }

                    if(mIconRes != 0) {
                        mImgIcon.setImageResource(mIconRes);
                    }else {
                        mImgIcon.setImageResource(R.drawable.xdialog_normal);
                    }

                    if(mNegativeOptionListener != null){
                        mNegativeOption.setOnClickListener(mNegativeOptionListener);
                    }

                    if(mPositiveOptionListener != null){
                        mPositiveOption.setOnClickListener(mPositiveOptionListener);
                    }

                    if(mPositiveTextColor != 0){
                        mPositiveOption.setTextColor(mPositiveTextColor);
                    }

                    if(mTitleTextColor != 0){
                        mTvTitle.setTextColor(mTitleTextColor);
                    }

                    if(mMessageTextColor != 0){
                        mTvMessage.setTextColor(mMessageTextColor);
                    }

                    // 设置对话框属性（宽高，显示位置，弹入退出动画）
                    setXDialogAttributes(dialog, view);
                    break;

                case TIP:
                    view = LayoutInflater.from(mContext).inflate(sLayoutResTip, null);
                    mTvMessage = view.findViewById(R.id.xdialog_message);
                    mPositiveOption = view.findViewById(R.id.positive_text);
                    mImgIcon = view.findViewById(R.id.img_icon);
                    if(mPositiveOptionListener != null){
                        mPositiveOption.setOnClickListener(mPositiveOptionListener);
                    }
                    if(!TextUtils.isEmpty(mMessage)){
                        mTvMessage.setText(mMessage);
                    }
                    if(mMessageTextColor != 0){
                        mTvMessage.setTextColor(mMessageTextColor);
                    }
                    if(mIconRes != 0) {
                        mImgIcon.setImageResource(mIconRes);
                    }
                    // 设置对话框属性（宽高，显示位置，弹入退出动画）
                    setXDialogAttributes(dialog, view);
                    break;
                default:
                    break;
            }
            return dialog;
        }

        /**
         * 对话框内容文本
         * @param message
         * @return
         */
        public XBuilder setMessage(String message){
            this.mMessage = message;
            return this;
        }

        /**
         * 设置取消按钮的点击事件
         * @param n_text
         * @param n
         * @return
         */
        public XBuilder setNegativeListener(String n_text , View.OnClickListener n){
            this.mNegativeText = n_text;
            this.mNegativeOptionListener = n;
            return this;
        }

        /**
         * 设置确定按钮的点击事件
         * @param p_text
         * @param p
         * @return
         */
        public XBuilder setPositiveListener(String p_text, View.OnClickListener p){
            this.mPositiveText = p_text;
            this.mPositiveOptionListener = p;
            return this;
        }

        /**
         * 设置标题文本
         * @param title
         * @return
         */
        public XBuilder setTitle(String title){
            this.mTitle = title;
            return this;
        }

        /**
         * 设置图标
         * @param iconRes
         * @return
         */
        public XBuilder setIcon(int iconRes){
            this.mIconRes = iconRes;
            return this;
        }

        /**
         * 设置属性，动画
         * @param dialog
         * @param view
         */
        protected void setXDialogAttributes(XDialog dialog, View view){
            Window window = dialog.getWindow();
            // 设置对话框的padding
            window.getDecorView().setPadding(0,0,0,0);
            WindowManager.LayoutParams lp = window.getAttributes();
            // 对话框的宽度
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
            // 对话框的高度
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

            window.setAttributes(lp);
            // 设置对话框的在屏幕中的位置，这里是居中
            window.setGravity(Gravity.CENTER);
            // 设置对话框弹入退出动画
            window.setWindowAnimations(R.style.dialog_enter_and_exit_animation_1);

            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(true);
            dialog.setCancelable(true);
        }

        /**
         * 设置字体颜色
         * @param colorRes
         * @return
         */
        public XBuilder setPositiveTextColor(int colorRes){
            this.mPositiveTextColor = colorRes;
            return this;
        }

        /**
         * 设置标题字体颜色
         * @param colorRes
         * @return
         */
        public XBuilder setTitleTextColor(int colorRes){
            this.mTitleTextColor = colorRes;
            return this;
        }

        /**
         * 设置内容字体颜色
         * @param colorRes
         * @return
         */
        public XBuilder setMessageTextColor(int colorRes){
            this.mMessageTextColor = colorRes;
            return this;
        }
    }

}
